package com.sea.finance.service.impl;


import com.codecrane.core.util.DateUtils;
import com.sea.finance.entity.Inventory;
import com.sea.finance.entity.PaymentChange;
import com.sea.finance.mapper.InventoryMapper;
import com.sea.finance.service.InventoryService;
import com.sea.finance.service.PaymentChangeService;
import com.sea.operate.entity.RelatedSku;
import com.sea.operate.service.RelatedSkuService;
import com.sea.physical.entity.CreateDespatchSku;
import com.sea.physical.service.CreateDespatchSkuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;


/**
 * @author 陈鸿飞
 */
@Service
public class InventoryServiceImpl implements InventoryService {

    @Autowired
    private InventoryMapper inventoryMapper;
    @Autowired
    private CreateDespatchSkuService createDespatchSkuService;
    @Autowired
    private RelatedSkuService relatedSkuService;
    @Autowired
    private PaymentChangeService paymentChangeService;


    @Override
    public List<Inventory> findByCnd(Inventory inventory) {
        return inventoryMapper.findByCnd(inventory);
    }

    @Override
    public int save(Inventory inventory) {
        if(null== inventory.getCreateCustomFreight()){
            inventory.setCreateCustomFreight(0.00);
        }
        return inventoryMapper.insert(inventory);
    }


    @Transactional(rollbackFor = Exception.class)
    @Override
    public void inventory(String month) throws Exception {
        List<RelatedSku> list = relatedSkuService.findByCnd(null);
        String yearMonth = DateUtils.getLastMonth();
        if (null != list && list.size() != 0) {
            for (RelatedSku relatedSku : list) {

                Inventory inventory = new Inventory();
                inventory.setInventorySku(relatedSku.getRelatedSku());
                inventory.setInventoryChannelName(relatedSku.getRelatedChannelName());
                inventory.setInventoryDate(month);


                //查询本期入库数据
                CreateDespatchSku createDespatchSku = createDespatchSkuService.findByMonthChannelSku(month, relatedSku.getRelatedChannelName(), relatedSku.getRelatedSku());
                 if (null != createDespatchSku && null != createDespatchSku.getCreateNumber()) {
                    inventory.setInventoryBorrowNumber(createDespatchSku.getCreateNumber());
                    inventory.setInventoryBorrowPrice(createDespatchSku.getErpProduceUnitPrice()+createDespatchSku.getCreateCustomTax()+createDespatchSku.getCreateCustomFreight());
                    inventory.setInventoryBorrowMoney(inventory.getInventoryBorrowNumber() * inventory.getInventoryBorrowPrice());
                    inventory.setErpProduceUnitPrice(createDespatchSku.getErpProduceUnitPrice());
                    inventory.setCreateCustomTax(createDespatchSku.getCreateCustomTax());
                    inventory.setCreateCustomFreight(createDespatchSku.getCreateCustomFreight());

                } else {
                    inventory.setInventoryBorrowNumber(0);
                    inventory.setInventoryBorrowPrice(0.00);
                    inventory.setInventoryBorrowMoney(0.00);
                    inventory.setErpProduceUnitPrice(0.00);
                    inventory.setCreateCustomTax(0.00);
                    inventory.setDespatchFreight(0.00);
                }

                //获取上个月份期末数据 即本月期初数据

                Inventory inv = new Inventory();
                inv.setInventorySku(relatedSku.getRelatedSku());
                inv.setInventoryChannelName(relatedSku.getRelatedChannelName());
                inv.setInventoryDate(yearMonth);
                Inventory preInventory = findBySkuChannelDate(inv);
                if (null == preInventory) {
                    inventory.setInventoryBeginNumber(0);
                    inventory.setInventoryBeginMoney(0.00);
                    inventory.setInventoryBeginPrice(0.00);
                    inventory.setInventoryBeginUnitPrice(0.00);
                    inventory.setInventoryBeginCustomTax(0.00);
                    inventory.setInventoryBeginCustomFreight(0.00);
                } else {
                    inventory.setInventoryBeginNumber(preInventory.getInventoryEndNumber());
                    inventory.setInventoryBeginMoney(((null==preInventory.getInventoryEndMoney())?0:preInventory.getInventoryEndMoney()));
                    inventory.setInventoryBeginPrice(((null==preInventory.getInventoryEndPrice())?0:preInventory.getInventoryEndPrice()));
                    inventory.setInventoryBeginUnitPrice(((null==preInventory.getInventoryEndUnitPrice())?0:preInventory.getInventoryEndUnitPrice()));
                    inventory.setInventoryBeginCustomTax(((null==preInventory.getInventoryEndCustomTax())?0:preInventory.getInventoryEndCustomTax()));
                    inventory.setInventoryBeginCustomFreight(((null==preInventory.getInventoryEndCustomFreight())?0:preInventory.getInventoryEndCustomFreight()));
                }

                inventory.setInventoryCreditNumber(0);
                inventory.setInventoryCreditMoney(0.00);
                inventory.setInventoryCreditPrice(0.00);
                inventory.setInventoryEndNumber(0);
                inventory.setInventoryEndMoney(0.00);
                inventory.setInventoryEndPrice(0.00);
                inventory.setInventoryEndUnitPrice(0.00);
                inventory.setInventoryEndCustomTax(0.00);
                inventory.setInventoryEndCustomFreight(0.00);
                save(inventory);
            }
        }
    }


    @Override
    public int updateCreditNumberBySkuChannelDate(Inventory inventory) {
        return inventoryMapper.updateCreditNumberBySkuChannelDate(inventory);
    }

    @Override
    public int updateEndBySkuChannelDate(Inventory inventory) {
        return inventoryMapper.updateEndBySkuChannelDate(inventory);
    }

    @Override
    public Inventory findBySkuChannelDate(Inventory inventory) {
        return inventoryMapper.findBySkuChannelDate(inventory);
    }

    @Override
    public List<Inventory> findSkuCost(Inventory inventory) {
        return inventoryMapper.findSkuCost(inventory);
    }

    @Override
    public Inventory findBySKuChannel(Inventory inventory) {
        return inventoryMapper.findBySKuChannel(inventory);
    }



    @Override
    public Integer usave(Inventory inventory) {
        return inventoryMapper.update(inventory);
    }
}
